﻿Imports DataAccess
Imports DTO

Public Class BSEmployee


    Dim strColID As String = "Registration No"
    Dim strColNo As String = "Employee No"
    Dim strColName As String = "Employee Name"
    Dim strColOrgan As String = "Organization"
    Dim strColTimetable As String = "Timetable"
    Dim strColPwd As String = "Password"
    Dim strColStartDate As String = "Start Date"
    Dim strColRetireDate As String = "Retirement Date"


    Public Sub New(ByVal strConnect As String)

        ' config server name
        DAEmployee.ConfigServerName(strConnect)

    End Sub

    ' ''' <summary>
    ' ''' Get employee by organizational No
    ' ''' </summary>
    ' ''' <param name="organNo"></param>
    ' ''' <returns></returns>
    ' ''' <remarks></remarks>
    'Public Function GetEmployee(ByVal organNo As String) As DataTable

    '    Return DAEmployee.GetEmployee(organNo)

    'End Function

    ''' <summary>
    ''' Return Datatable employee change name data column
    ''' </summary>
    ''' <param name="dtEmployee"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetDataSource(ByVal dtEmployee As DataTable) As DataTable

        Dim dataTable As DataTable = New DataTable()

        dataTable.Columns.Add(strColID)
        dataTable.Columns.Add(strColNo)
        dataTable.Columns.Add(strColName)
        dataTable.Columns.Add(strColPwd)
        dataTable.Columns.Add(strColOrgan)
        dataTable.Columns.Add(strColTimetable)
        dataTable.Columns.Add(strColStartDate)
        dataTable.Columns.Add(strColRetireDate)
        dataTable.Columns.Add("Status")

        Dim newRow As DataRow

        For Each row As DataRow In dtEmployee.Rows

            newRow = dataTable.NewRow()

            newRow.Item(strColID) = row.Item("ID")
            newRow.Item(strColNo) = row.Item("EMPLOYEE_NO")
            newRow.Item(strColName) = row.Item("LAST_NAME").ToString() & " " & row.Item("FIRST_NAME").ToString()
            newRow.Item(strColOrgan) = row.Item("ORGANIZATIONAL_NAME")
            newRow.Item(strColTimetable) = row.Item("TIME_TABLE_NAME")

            If Not row.Item("EXPIRATION_FROM").ToString().Equals("0") Then

                newRow.Item(strColStartDate) = Functions.ConvertstrDBDateToStrDate(row.Item("EXPIRATION_FROM"))

            Else

                newRow.Item(strColStartDate) = Nothing

            End If

            If Not row.Item("EXPIRATION_TO").ToString().Equals("99999999") Then

                newRow.Item(strColRetireDate) = Functions.ConvertstrDBDateToStrDate(row.Item("EXPIRATION_TO"))

            Else

                newRow.Item(strColRetireDate) = Nothing

            End If

            newRow.Item("Status") = row.Item("STATUS")

            If Not IsDBNull(row.Item("LOGIN_PASSWORD")) Then

                newRow.Item(strColPwd) = "******"

            Else

                newRow.Item(strColPwd) = Nothing

            End If

            dataTable.Rows.Add(newRow)

        Next

        Return dataTable
        
    End Function


    Public Function GetTableIdNotReg() As DataTable

        Dim dtIdTer As DataTable = DAEmployee.GetIdTerminalUserData()

        Dim dtIdEmp As DataTable = DAEmployee.GetIdEmployee()

        Dim dataTable As DataTable = New DataTable()

        dataTable.Columns.Add(strColID)
        dataTable.Columns.Add(strColNo)
        dataTable.Columns.Add(strColName)
        dataTable.Columns.Add(strColPwd)
        dataTable.Columns.Add(strColOrgan)
        dataTable.Columns.Add(strColTimetable)
        dataTable.Columns.Add(strColStartDate)
        dataTable.Columns.Add(strColRetireDate)
        dataTable.Columns.Add("Status")

        Dim newRow As DataRow

        Dim flag As Boolean

        For Each rowTer As DataRow In dtIdTer.Rows

            flag = True

            For Each rowEmp As DataRow In dtIdEmp.Rows

                If rowTer.Item("ID").ToString().Equals(rowEmp.Item("ID").ToString()) Then

                    flag = False

                    Exit For

                End If
                
            Next

            ' check if ID did not exist in Employee
            If flag Then

                newRow = dataTable.NewRow()

                newRow.Item(strColID) = rowTer.Item("ID")
                newRow.Item(strColNo) = Nothing
                newRow.Item(strColName) = Nothing
                newRow.Item(strColOrgan) = Nothing
                newRow.Item(strColTimetable) = Nothing
                newRow.Item(strColStartDate) = Nothing
                newRow.Item(strColRetireDate) = Nothing
                newRow.Item("Status") = Nothing

                dataTable.Rows.Add(newRow)

            End If
            
        Next


        Return dataTable


    End Function

    ''' <summary>
    ''' get employee by no name and organization No
    ''' </summary>
    ''' <param name="organNo"></param>
    ''' <param name="str"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetEmployee(ByVal organNo As String, ByVal str As String) As DataTable

        Return DAEmployee.GetEmployee(organNo, str)

    End Function


    ''' <summary>
    ''' get employee information, return DTOEmployee
    ''' </summary>
    ''' <param name="id"></param>
    ''' <param name="no"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetEmpByIdNo(ByVal id As String, ByVal no As String) As DTOEmployee

        Dim dt As DataTable = DAEmployee.GetEmpByIdNo(id, no)

        Dim dtoEmployee As DTOEmployee = New DTOEmployee()

        dtoEmployee.IdProperty = id

        dtoEmployee.EmpNoProperty = no

        dtoEmployee.ExpFromProperty = dt.Rows.Item(0).Item("EXPIRATION_FROM")

        dtoEmployee.ExpToProperty = dt.Rows.Item(0).Item("EXPIRATION_TO")

        dtoEmployee.OrgNoProperty = dt.Rows.Item(0).Item("ORGANIZATIONAL_NO")

        dtoEmployee.TimNoProperty = dt.Rows.Item(0).Item("TIME_TABLE_NO")

        dtoEmployee.ExpTimFromProperty = dt.Rows.Item(0).Item("EXPIRATION_FROM_TIME_TABLE")

        dtoEmployee.ExpTimToProperty = dt.Rows.Item(0).Item("EXPIRATION_TO_TIME_TABLE")

        dtoEmployee.FirNameProperty = dt.Rows.Item(0).Item("FIRST_NAME")

        dtoEmployee.LasNameProperty = dt.Rows.Item(0).Item("LAST_NAME")

        If Not IsDBNull(dt.Rows.Item(0).Item("DAY_OF_BIRTH")) Then

            dtoEmployee.DatOfBirthProperty = dt.Rows.Item(0).Item("DAY_OF_BIRTH")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("ID_CARD")) Then

            dtoEmployee.IdCardProperty = dt.Rows.Item(0).Item("ID_CARD")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("EMAIL")) Then

            dtoEmployee.EmailProperty = dt.Rows.Item(0).Item("EMAIL")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("GENDER")) Then

            dtoEmployee.GenderProperty = dt.Rows.Item(0).Item("GENDER")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("CELL_PHONE")) Then

            dtoEmployee.CelPhoneProperty = dt.Rows.Item(0).Item("CELL_PHONE")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("HOME_PHONE")) Then

            dtoEmployee.HomPhoneProperty = dt.Rows.Item(0).Item("HOME_PHONE")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("CURRENT_ADDRESS")) Then

            dtoEmployee.CurrAddressProperty = dt.Rows.Item(0).Item("CURRENT_ADDRESS")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("LOGIN_PASSWORD")) Then

            dtoEmployee.LoginPwdProperty = dt.Rows.Item(0).Item("LOGIN_PASSWORD")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("REGISTRATION_ADDRESS")) Then

            dtoEmployee.RegAddressProperty = dt.Rows.Item(0).Item("REGISTRATION_ADDRESS")

        End If

        If Not IsDBNull(dt.Rows.Item(0).Item("MARITAL_STATUS")) Then

            dtoEmployee.MarStatusProperty = dt.Rows.Item(0).Item("MARITAL_STATUS")

        End If

        dtoEmployee.CreDateProperty = dt.Rows.Item(0).Item("CREATE_DATE_TIME")

        dtoEmployee.UpdDateProperty = dt.Rows.Item(0).Item("UPDATE_DATE_TIME")

        Return dtoEmployee

    End Function


    ''' <summary>
    ''' Delete employee with expiration date to = 99999999
    ''' </summary>
    ''' <param name="id"></param>
    ''' <param name="no"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteEmployee(ByVal id As String, ByVal no As String, ByVal exTo As string) As Boolean

        If DAEmployee.DeleteEmployee(id, no, exTo) <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Delete employee with expiration date <> 99999999
    ''' </summary>
    ''' <param name="id"></param>
    ''' <param name="no"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteEmployeeWithoutExTo(ByVal id As String, ByVal no As String) As Boolean

        If DAEmployee.DeleteEmployeeWithoutExTo(id, no) <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Check if employee no is existed in database, return true if duplicate, otherwise.
    ''' </summary>
    ''' <param name="empNo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function CheckDuplicateEmpNo(ByVal empNo As String) As Boolean

        Dim dtEmployees As DataTable = DAEmployee.GetAllEmployee()

        For Each row As DataRow In dtEmployees.Rows

            If row.Item("EMPLOYEE_NO").ToString().Equals(empNo) Then

                Return True

            End If

        Next

        Return False

    End Function

    ''' <summary>
    ''' Add employee
    ''' </summary>
    ''' <param name="dtoEmployee"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function AddEmployee(ByVal dtoEmployee As DTOEmployee) As Boolean

        If DAEmployee.AddEmployee(dtoEmployee) <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Modify employee
    ''' </summary>
    ''' <param name="dtoEmployee"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ModifyEmployee(ByVal dtoEmployee As DTOEmployee) As Boolean

        If DAEmployee.ModifyEmployee(dtoEmployee) <> 0 Then

            Return True

        Else

            Return False

        End If

    End Function

    ''' <summary>
    ''' Get employeeNo from ID Enroll No
    ''' </summary>
    ''' <param name="dtoEmployee"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetEmployeeNoFromEnrollNo(ByVal enrollNo As Integer) As String
        Dim dtoEmployee As New DTOEmployee
        dtoEmployee.IdProperty = enrollNo
        '  Dim daEmployee As New DAEmployee
        Return DAEmployee.GetEmployeeNoFromEnrollNo(dtoEmployee)
    End Function

    ''' <summary>
    ''' Check Username and Password
    ''' </summary>
    ''' <param name="dtoEmployee"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CheckUsernameAndPassword(ByVal employeeNo As String, ByVal loginPassword As String) As Integer
        If employeeNo = "admin" And loginPassword = "admin" Then
            Return 1
        Else
            Dim dtoEmployee As New DTOEmployee
            dtoEmployee.EmpNoProperty = employeeNo
            dtoEmployee.LoginPwdProperty = loginPassword
            'Dim daEmployee As New DAEmployee
            Return DAEmployee.CheckUsernameAndPassword(dtoEmployee)
        End If

    End Function

    ''' <summary>
    ''' Open connection 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub OpenConnect()

        DAEmployee.OpenConnect()

    End Sub


    ''' <summary>
    ''' Close connection 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub CloseConnect()

        DAEmployee.CloseConnect()

    End Sub
End Class
